﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace SynoManager.DownloadStation.Search
{
    public partial class SearchGuiTab : UserControl
    {
        private readonly SearchGuiPlugin plugin;

        public SearchGuiTab()
        {
            InitializeComponent();
            Text = "Search";
        }

        public SearchGuiTab(SearchGuiPlugin plugin)
            :this()
        {
            this.plugin = plugin;

            enginesComboBox.DataSource = plugin.Manager.Engines;
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            ISearchEngine engine = (ISearchEngine) enginesComboBox.SelectedItem;
            string query = queryTextBox.Text;

            StartSearch(engine, query);
        }

        private void StartSearch(ISearchEngine engine, string query)
        {
            if (backgroundWorker.IsBusy)
                return;

            resultListView.Items.Clear();
            searchButton.Enabled = false;

            backgroundWorker.RunWorkerAsync(new QueryInfo() { Engine = engine, Query = query });
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            QueryInfo info = (QueryInfo) e.Argument;
            e.Result = info.Engine.Search(info.Query);
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ISearchResult[] results = (ISearchResult[]) e.Result;
            foreach(ISearchResult result in results)
            {
                ListViewItem lvi = resultListView.Items.Add(result.Title, "result");
                lvi.SubItems.Add(result.Category);
                lvi.SubItems.Add(result.Size.ToByteString());
                lvi.SubItems.Add(result.Seeders.ToString());
                lvi.SubItems.Add(result.Leechers.ToString());
                lvi.Tag = result;
            }
            searchButton.Enabled = true;
        }

        struct QueryInfo
        {
            public ISearchEngine Engine;
            public string Query;
        }

        #region ContextMenu
        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            e.Cancel = resultListView.SelectedItems.Count == 0;
            downloadToolStripMenuItem.Enabled = plugin.service.Device.IsConnected;
        }

        private void downloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach(ListViewItem lvi in resultListView.SelectedItems)
            {
                ISearchResult result = lvi.Tag as ISearchResult;
                plugin.service.AddUrl(result.DownloadUrl);
                lvi.ImageKey = "download";
            }
        }

        private void viewWebsiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in resultListView.SelectedItems)
            {
                ISearchResult result = lvi.Tag as ISearchResult;
                string uri = result.MoreInfoUrl;
                Process.Start(uri.ToString());
            }
        } 
        #endregion
    }
}
